/**
******************************************************************************************
* @file AverageMaker.c
* @brief Calculate the average over time.
* @date 23 avril 2015
* @author Benjamin Lancinot
******************************************************************************************/
/* COPYRIGHT ****************************************************************************
*****************************************************************************************
* Copyright (c) 2015, Audrey LOISEAU and Benjamin LANCINOT.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
*    list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
*    this list of conditions and the following disclaimer in the documentation
*    and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
********************************************************************************************/

#include "AverageMaker.h"


/*******************************************************************************************
* PROTOYPES
********************************************************************************************/

void AverageMaker_init(AverageMaker*, float);

void AverageMaker_reset(AverageMaker*);

void AverageMaker_averageMAJ(AverageMaker*, float);

void AverageMaker_destroyUtilities(AverageMaker*);

void AverageMaker_destroy(AverageMaker**);

int AverageMaker_registerObserver(AverageMaker*, Observer*);

int AverageMaker_unregisterObserver(AverageMaker*, Observer*);


/*******************************************************************************************
 * FUNCTIONS
 ********************************************************************************************/

/**
 * @brief Constructor
 * @param mDaddy Type of average can be Temperature(0) or Humidity(1)
 * @return Pointer to a new AverageMaker
 **/
AverageMaker* AverageMaker_create(int mDaddy){
	AverageMaker* This = (AverageMaker *) calloc(1,sizeof(*This));

        This->daddy = mDaddy;

        //Init functions
        This->destroy = AverageMaker_destroy;
        This->averageMAJ = AverageMaker_averageMAJ;
        This->init =  AverageMaker_init;
        This->reset =  AverageMaker_reset;
	This->subject = subjectNew(This, 7);
	This->registerObserver = AverageMaker_registerObserver;
	This->unregisterObserver = AverageMaker_unregisterObserver;

	return This;
}


/**
 * @brief Initialisation of an average with good value
 * @param This Pointer to the wanted AverageMaker
 * @param newData value of the first data in the average
 **/
void AverageMaker_init(AverageMaker* This, float newData){

        This->reset(This);
        This->previousData = newData;
        This->previousAverageSaved = 0;
}

/**
 * @brief Reset the average
 * @param This Pointer to the wanted AverageMaker
 **/
void AverageMaker_reset(AverageMaker* This){

        time(&This->previousTime);
	This->totalTime =0;
        This->averageNumerator = 0;
}


/**
 * @brief Calculate weighted average with new data over the time
 * @param This Pointer to the wanted AverageMaker
 * @param newData value of a new data to add on the average
 **/
void AverageMaker_averageMAJ(AverageMaker* This, float newData){
        time_t currentTime;
        float interval;

	time(&currentTime);
	interval = difftime(This->previousTime,currentTime);
        This->previousTime = currentTime;

        This->totalTime += interval;
	This->averageNumerator += This->previousData * interval;


	if ( (((This->averageNumerator / This->totalTime)+ 0.1) < This->previousAverageSaved) || (((This->averageNumerator/ This->totalTime) - 0.1) > This->previousAverageSaved) ){
		This->subject->notifyObservers(This->subject);
                This->previousAverageSaved = (This->averageNumerator / This->totalTime);
	}
        This->previousData = newData;

}


/**
 * @brief Destroy all of the components of AverageMaker
 * @param This Pointer to the wanted AverageMaker
 **/
void AverageMaker_destroyUtilities(AverageMaker* This){
	This->subject->destroy(This->subject);
}

/**
 * @brief Destructor
 * @param This Double pointer to the wanted AverageMaker
 **/
void AverageMaker_destroy(AverageMaker** This){
        AverageMaker_destroyUtilities(* This);
	if (*This) {
		free(*This);
		*This = NULL;
	}
}

/**
 * @brief Proxy method for Register an observer for the AverageMaker object.
 * @param This Pointer to the wanted AverageMaker
 * @param This Pointer to the wanted observer
 * @return The result of the development of the registration (0 = GOOD and 1 = NOT GOOD)
 **/
int AverageMaker_registerObserver(AverageMaker* This, Observer* observer) {
	return This->subject->registerObserver(This->subject, observer);
}

/**
 * @brief Proxy method for Unregister observer for the AverageMaker object.
 * @param This Pointer to the wanted AverageMaker
 * @param This Pointer to the wanted observer
 * @return The result of the development of the unregistration (0 = GOOD and 1 = NOT GOOD)
 **/
int AverageMaker_unregisterObserver(AverageMaker* This, Observer* observer) {
	return This->subject->unregisterObserver(This->subject, observer);
}
